<?php namespace Common\Model;
use Hdphp\Model\Model;
class Orders extends Model{
	protected $table = 'orders';
/*//	注册信息的自动验证
	protected $validate = array(
		array('username','required','用户名不能为空',3,3),
		array('username','user:5,20','用户名不能少于5个且不能超过20个字符',3,3),
		array('password','required','密码不能为空',3,3),
		array('password','minlen:5','密码不能少于5位',3,3),
		array('password','regexp:/^[A-Za-z0-9_-]+$/is ','密码只能是数字，字母，下划线',3,3),
		array('password2','required','验证密码不能为空',3,3),
		array('password2','minlen:5','密码不能少于5位',3,3),
		array('email','required','邮箱不能为空',3,3),
		array('email','email','输入的邮箱不正确',3,3)
	);
//	自动完成注册时间
	protected $auto = array(
		array('regtime','time','function',3,1)
	);
	*/
//	用户注册
	public function store(){
//		1.生成订单号
		$this->data['onumber'] = 'legou' . time();
//		2.组合收货人信息
		$adid = Q('post.address_id',0,'intval');
		$addrModel = new \Common\Model\Address;
		$addrData = $addrModel->where("adid = {$adid}")->find();
		$this->data['consignee'] = $addrData['consignee'];
		$this->data['tel'] = $addrData['tel'];
		$this->data['address'] = $addrData['province'].$addrData['city'].$addrData['district'].$addrData['detail'];
//		3.订单生成时间
		$this->data['time'] = time();
//		4.用户id
		$this->data['user_uid'] = $_SESSION['uid'];
//		5.价格总计
		$this->data['total'] = 0;
		$goodModel = new \Common\Model\Goods;
		$num = Q('post.num');
		foreach (Q('post.gid') as $k => $v) {
			$price = $goodModel->where("gid = {$v}")->pluck('sprice');
			$this->data['total'] = $this->data['total'] + $price*$num[$k];
		}
		$this->data['status'] = 0;
		return $this->add();
	}
	
//	删除订单
	public function del(){
		$oid = Q('get.oid',0,'intval');
		$this->where("oid = $oid")->delete();
		Db::table('orderlist')->where("orders_oid = $oid")->delete();
		return true;
	}
//	确认首款
	public function pay(){
		$oid = Q('get.oid',0,'intval');
		$temp = array('status'=>1);
		$this->where("oid = $oid")->save($temp);
		return true;
	}
//	确认发货
	public function shipment(){
		$oid = Q('get.oid',0,'intval');
		$temp = array('status'=>2);
		$this->where("oid = $oid")->save($temp);
		return true;
	}
	public function haverec(){
		$oid = Q('get.oid',0,'intval');
		$temp = array('status'=>3);
		$this->where("oid = $oid")->save($temp);
		return true;
	}

//	消除24小时未支付的订单
	public function removeorder(){
		$lastTime = intval(strtotime('-1 day'));
		$data = $this->where("status = 0")->get();
		foreach ($data as $k => $v) {
			if($v['time']<$lastTime){
				$this->where("oid = {$v['oid']}")->save(array('status'=>4));
			}
		}
	}









}
 ?>